/**
* @program: src
*
* @description:
*
* @author: Mr.chen
*
* @create: 2020-12-18 16:58
**/
package config
import "github.com/spf13/viper"

// ******************api设置开始*********************
// api基础信息
type ApiBase struct {
	Port         string `mapstructure:"port"`
	DebugMode    bool   `mapstructure:"debug_mode"`
	ServiceUrl   string `mapstructure:"service_url"`
	RpcUrl       string `mapstructure:"rpc_url"`
	MaxPageSize  int    `mapstructure:"max_page_size"`
	IsPublicSign bool   `mapstructure:"is_public_sign"`
}

// api普通信息
type ApiCommon struct {
	Cluster   bool   `mapstructure:"cluster"`
	CryptoKey string `mapstructure:"crypto_key"`
}

// etcd配置信息
type ApiEtcd struct {
	Host string `mapstructure:"host"`
}

// 数据库注册驱动
type ApiDbCommon struct {
	Runmode   string `mapstructure:"runmode"`
	EnableLog bool   `mapstructure:"enable_log"`
	Dialect   string `mapstructure:"dialect"`
}

// api数据库配置
type ApiDbConf struct {
	Host               string
	User               string
	PassWd             string
	Db                 string
	Port               int64
	MaxOpenConnections int
	MaxIdleConnections int
	ConnMaxLifetime    int
}

// 主数据库配置信息
type ApiDbWdb struct {
	Host   string `mapstructure:"w_host"`
	User   string `mapstructure:"w_user"`
	PassWd string `mapstructure:"w_pass"`
	Db     string `mapstructure:"w_db"`
	Port   int64  `mapstructure:"w_port"`
}

// 从数据库配置信息
type ApiDbRdb struct {
	Host   string `mapstructure:"r_host"`
	User   string `mapstructure:"r_user"`
	PassWd string `mapstructure:"r_pass"`
	Db     string `mapstructure:"r_db"`
	Port   int64  `mapstructure:"r_port"`
}

// api日志配置
type LogConfig struct {
	Level      string `mapstructure:"level"`
	Filename   string `mapstructure:"filename"`
	MaxSize    int    `mapstructure:"maxsize"`
	MaxAge     int    `mapstructure:"max_age"`
	MaxBackups int    `mapstructure:"max_backups"`
}
// api-jagger配置
type JaggerConfig struct {
	Name      string `mapstructure:"name"`
	Endpoint   string `mapstructure:"endpoint"`
}
// api总的配置信息
type ApiConfig struct {
	ApiBase     ApiBase     `mapstructure:"api-base"`
	ApiCommon   ApiCommon   `mapstructure:"api-common"`
	ApiEtcd     ApiEtcd     `mapstructure:"api-etcd"`
	ApiDbCommon ApiDbCommon `mapstructure:"api-db-common"`
	ApiDbWdb    ApiDbWdb    `mapstructure:"api-db-wdb"`
	ApiDbRdb    ApiDbRdb    `mapstructure:"api-db-rdb"`
	ApiZaplog   LogConfig   `mapstructure:"api-zaplog"`
	ApiDbconfig map[string][]ApiDbConf
	JaggerConfig JaggerConfig   `mapstructure:"api-jaeger"`
}



// 获取主从数据库相关配置6++
func GetApiDbConf() (DatabaseList map[string][]ApiDbConf) {
	var wdbArr, rdbArr []ApiDbConf
	wdb := ApiDbConf{
		Host:               viper.GetString("api-db-wdb.w_host"),
		User:               viper.GetString("api-db-wdb.w_user"),
		PassWd:             viper.GetString("api-db-wdb.w_pass"),
		Db:                 viper.GetString("api-db-wdb.w_db"),
		Port:               viper.GetInt64("api-db-wdb.w_port"),
		MaxOpenConnections: viper.GetInt("api-db-wdb.w_maxOpenConnections"),
		MaxIdleConnections: viper.GetInt("api-db-wdb.w_maxIdleConnections"),
		ConnMaxLifetime:    viper.GetInt("api-db-wdb.w_connMaxLifetime"),
	}
	wdbArr = append(wdbArr, wdb)
	rdb := ApiDbConf{
		Host:               viper.GetString("api-db-rdb.r_host"),
		User:               viper.GetString("api-db-rdb.r_user"),
		PassWd:             viper.GetString("api-db-rdb.r_pass"),
		Db:                 viper.GetString("api-db-rdb.r_db"),
		Port:               viper.GetInt64("api-db-rdb.r_port"),
		MaxOpenConnections: viper.GetInt("api-db-rdb.r_maxOpenConnections"),
		MaxIdleConnections: viper.GetInt("api-db-rdb.r_maxIdleConnections"),
		ConnMaxLifetime:    viper.GetInt("api-db-rdb.r_connMaxLifetime"),
	}
	rdbArr = append(rdbArr, rdb)
	return map[string][]ApiDbConf{
		"w": wdbArr,
		"r": rdbArr,
	}
}
// 获取api日记信息
func GetJaggerConfig() *JaggerConfig {
	return &Conf.ApiConfig.JaggerConfig
}